import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_input_page.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/ext/string_extension.dart';
import 'package:enterprise_v6_app/app/core/values/colors.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/risk_control_page/risk_security_identification_page/risk_on_click_add_event_page/risk_on_click_add_event_page_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class RiskOnClickAddEventPagePage extends StatelessWidget {
  RiskOnClickAddEventPagePage({super.key});

  final controller = Get.put(RiskOnClickAddEventPageController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          backgroundColor: AppColors.bg_color_grey,
          appBar: CustomAppBar(
            titleText: controller.addAndUpdate ? '新增' : '编辑',
          ),
          body: SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(top: 10.w),
                  padding: EdgeInsets.only(left: 16.w, right: 20.w),
                  color: Colors.white,
                  child: Column(
                    children: [
                      CustomInputPage(
                        title: '风险事件名称：',
                        isRequired: true,
                        isEnabled: true,
                        text: controller.eventName,
                        onCallBack: (value) {
                          controller.eventName = value;
                        },
                      ),
                      CustomInputPage(
                        title: '初始后果：',
                        isRequired: true,
                        hintText: '请输入小于等于5的数',
                        textInputType: TextInputType.number,
                        isEnabled: true,
                        text: controller.initialConsequences,
                        onCallBack: (value) {
                          if (value.isNotEmpty && double.parse(value) > 5) {
                            controller.initialConsequences = '';
                            controller.initialRiskLevel = '';
                            controller.currencyDangerLevelName = '';
                            controller.isBool();
                            AppToast.showWarning('请输入小于等于5的数');
                            return;
                          }
                          controller.initialConsequences = value;
                          controller.initialRiskLevel =
                              '${double.parse(controller.initialConsequences.isEmpty ? '0' : controller.initialConsequences) * double.parse(controller.initialPossibility.isEmpty ? '0' : controller.initialPossibility)}'
                                  .formatStringAsFixed();
                          controller.currencyDangerLevelId = controller.backRiskLevelId(double.parse(controller.initialRiskLevel));
                          controller.currencyDangerLevelName = controller.backRiskLevelName(double.parse(controller.initialRiskLevel));
                          controller.isBool();
                        },
                      ),
                      CustomInputPage(
                        title: '初始可能性：',
                        isRequired: true,
                        textInputType: TextInputType.number,
                        isEnabled: true,
                        text: controller.initialPossibility,
                        hintText: '请输入小于等于5的数',
                        onCallBack: (value) {
                          if (value.isNotEmpty && double.parse(value) > 5) {
                            controller.initialPossibility = '';
                            controller.initialRiskLevel = '';
                            controller.currencyDangerLevelName = '';
                            controller.isBool();
                            AppToast.showWarning('请输入小于等于5的数');
                            return;
                          }
                          controller.initialPossibility = value;
                          controller.initialRiskLevel =
                              '${double.parse(controller.initialConsequences.isEmpty ? '0' : controller.initialConsequences) * double.parse(controller.initialPossibility.isEmpty ? '0' : controller.initialPossibility)}'
                                  .formatStringAsFixed();
                          controller.currencyDangerLevelId = controller.backRiskLevelId(double.parse(controller.initialRiskLevel));
                          controller.currencyDangerLevelName = controller.backRiskLevelName(double.parse(controller.initialRiskLevel));
                          controller.isBool();
                        },
                      ),
                      CustomInputPage(
                        title: '初始风险度：',
                        isRequired: true,
                        isEnabled: false,
                        text: controller.initialRiskLevel == '0.00' ? '' : controller.initialRiskLevel,
                        onCallBack: (value) {
                          controller.initialRiskLevel = value;
                        },
                      ),
                      CustomInputPage(
                        title: '初始风险等级：',
                        isRequired: true,
                        isEnabled: false,
                        text: controller.currencyDangerLevelName,
                        onCallBack: (value) {},
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          bottomSheet: Container(
            width: double.infinity,
            color: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                SizedBox(
                  width: 120.w,
                  height: 40.w,
                  child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                          backgroundColor: const Color(0xffFFFFFF),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(22.r),
                          ),
                          side: BorderSide(
                            color: const Color(0xff5C6DFF),
                            width: 1.w, // 设置边框线的宽度
                          )),
                      onPressed: Get.back,
                      child: Text(
                        '取消',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: const Color(0xff5C6DFF),
                          fontWeight: FontWeight.w400,
                        ),
                      )),
                ),
                SizedBox(
                  width: 202.w,
                  height: 40.w,
                  child: ElevatedButton(
                      style: ElevatedButton.styleFrom(
                        backgroundColor: AppColors.blue_button,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(22.r),
                        ),
                      ),
                      onPressed: controller.addEventObject,
                      child: Text(
                        '确定',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: Colors.white,
                          fontWeight: FontWeight.w400,
                        ),
                      )),
                )
              ],
            ),
          ),
        );
      },
    );
  }
}
